package org.wzq.controller;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
import org.wzq.biz.Section;
import org.wzq.biz.Topic;
import org.wzq.biz.User;
import org.wzq.dao.SectionDao;
import org.wzq.dao.TopicDao;
import org.wzq.dao.UserDao;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * project: spring-mvc-forum
 * ate: 14-9-16
 * author: wzq
 * description: TODO
 */
@Controller
@RequestMapping("/")
public class Home {
    protected final Log logger = LogFactory.getLog(getClass());
    @Autowired
    private UserDao userDao;

    @Autowired
    private SectionDao sectionDao;

    @Autowired
    private TopicDao topicDao;

    @RequestMapping(method = {RequestMethod.POST, RequestMethod.GET})
    public ModelAndView home(HttpServletRequest req, HttpServletResponse res) throws IOException {
        logger.info("path=/, method=" + req.getMethod() + ", ");
        return new ModelAndView("home");
    }

    @RequestMapping(value = "sections", method = {RequestMethod.GET})
    public ModelAndView section(HttpServletRequest req, HttpServletResponse res) throws IOException {
        logger.info("path=/sections, method=" + req.getMethod() + ", ");
        List<Section> sectionList = sectionDao.findAll();

        return new ModelAndView("sections", "sectionList", sectionList);
    }

    @RequestMapping(value = "section/{sectionId}", method = {RequestMethod.GET})
    public ModelAndView section(@PathVariable String sectionId, HttpServletRequest req, HttpServletResponse res) throws IOException {
        logger.info("path=/section/sectionId, method=" + req.getMethod() + ", ");
        Section section = sectionDao.getSectionById(sectionId);

        List<Topic> list = topicDao.allTopinInSection(section.getSectionId());
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("section", section);
        map.put("topicList", list);
        return new ModelAndView("section", "map", map);
    }

    @RequestMapping(value = "section/{sectionId}/article", method = {RequestMethod.GET})
    public ModelAndView writeArticle(@PathVariable String sectionId, HttpServletRequest req, HttpServletResponse res) throws IOException {
        logger.info("path=/section/sectionId/postArticle, method=" + req.getMethod() + ", ");
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("sectionId", sectionId);
        return new ModelAndView("article", "map", map);
    }

    @RequestMapping(value = "section/{sectionId}/article", method = {RequestMethod.POST})
    public ModelAndView postArticle(@PathVariable String sectionId, HttpServletRequest req, HttpServletResponse res) throws IOException {
        logger.info("path=/section/sectionId/postArticle, method=" + req.getMethod() + ", ");
        String title = req.getParameter("title");
        String content = req.getParameter("content");

        // 保存文章
        Topic topic = new Topic(Integer.parseInt(sectionId), 1, title, content);
        topicDao.saveOrUpdate(topic);
        // update 版块 文章数 +1
        Section section = sectionDao.getSectionById(sectionId);
        section.setTopicNum(section.getTopicNum() + 1);
        sectionDao.saveOrUpdate(section);

        return new ModelAndView("redirect:/section/" + sectionId);
    }


    @RequestMapping(value = "topic/{topicId}", method = {RequestMethod.GET})
    public ModelAndView topic(@PathVariable String topicId, HttpServletRequest req, HttpServletResponse res) throws IOException {
        logger.info("path=/topic/topicId, method=" + req.getMethod() + ", ");
        System.out.println("topicId = " + topicId);
        Topic topic = topicDao.getTopic(topicId);
        topic.setViewNum(topic.getViewNum() + 1);
        topicDao.saveOrUpdate(topic);
        List<Topic> replys = topicDao.allTopinReply(topic.getTopicId());
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("topic", topic);
        map.put("reply", replys);
        return new ModelAndView("topic", "map", map);
    }

    @RequestMapping(value = "user/login", method = {RequestMethod.GET})
    public ModelAndView login(HttpServletRequest req, HttpServletResponse res) throws IOException {
        logger.info("path=/user/login, method=" + req.getMethod() + ", ");
        return new ModelAndView("login");
    }

    @RequestMapping(value = "register", method = {RequestMethod.GET})
    public ModelAndView register(HttpServletRequest req, HttpServletResponse res) throws IOException {
        logger.info("path=/register, method=" + req.getMethod() + ", ");
        return new ModelAndView("register");
    }

    @RequestMapping(value = "manager", method = {RequestMethod.GET})
    public ModelAndView manager(HttpServletRequest req, HttpServletResponse res) throws IOException {
        logger.info("path=/manager, method=" + req.getMethod() + ", ");
        return new ModelAndView("manager");
    }

    @RequestMapping(value = "users", method = {RequestMethod.GET})
    public ModelAndView users(HttpServletRequest req, HttpServletResponse res) throws IOException {
        logger.info("path=/users, method=" + req.getMethod() + ", ");
        List<User> userList = userDao.findAll();

        return new ModelAndView("users", "userList", userList);
    }


    @RequestMapping(value = "register_submit", method = {RequestMethod.POST})
    public ModelAndView registerSubmit(HttpServletRequest req, HttpServletResponse res) throws IOException {
        logger.info("path=/register_submit, method=" + req.getMethod() + ", ");
        String name = req.getParameter("name");
        // TODO check name already registered ? ajax ??

        String password = req.getParameter("password");
        String sex = req.getParameter("sex");
        User user = new User(name, sex, password);
        System.out.println("user = " + user);

        // insert into db
        userDao.addUser(user);

        return new ModelAndView("register_succ");
    }
}
